<?php

namespace admin\controller;

use admin\model\TypeModel;

class TypeController extends Controller{
    
    private $type = null;

    public function __construct(){
        $this->type = new TypeModel();
    }
    //查找指定id的并调用viwe显示
    public function getType($params){
        $this->setData($this->type->getType($params['id']));
        return $this->view('index');
    }
    //查询数据库中的所信息并调用viwe显示
    public function getTypeAll(){
        // $this->setData('types',$this->type->getTypeAll());
        return $this->view('manageType');       //显示界面
    }
    //查询所有的信息以 JSON 形式返回
    public function getTypesJson($params){
        $data = $this->type->getTypeAll($params);
        $count_data = $this->type->getTypeCount(array('name'=>$params['name']));
        $data_json = [];
        $data_json['code'] = 0;
        $data_json['msg'] = '';
        $data_json['count'] = $count_data['count'];
        $data_json['data'] = $data;
        return json_encode($data_json);
    }
    /**
     * 设置类别状态为禁用
     */
    public function setTypeBan($params){
        $data = $this->type->banType($params);  //操作数据库并返回状态嘛
        return json_encode($data);      //返回给前端 ajax 
    }
    /**
     * 修改类别信息
     */
    public function editType($params){
        $data = $this->type->updateType($params);
        return json_encode($data);
    }
    /**
     * 添加类别信息
     */
    public function addType($params){
        $data = $this->type->insertType($params);
        return json_encode($data);
    }
    /**
     * 删除类别信息
     */
    public function delType($params){
        $data = $this->type->delType($params);
        return json_encode($data);
    }
}
